package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        String queryString1 = queryPageBean.getQueryString1();
//        基于mybatis分页助手
        PageHelper.startPage(currentPage, pageSize);//绑定当前线程
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString,queryString1);
        // PageInfo<T> tPageInfo = new PageInfo<>(page);
        long total = page.getTotal();//总记录数
        List<CheckItem> rows = page.getResult();//每页数据
        return new PageResult(total, rows);
    }

    @Override
    public Result deleteById(Integer id) {
//        查询数据是否关联检查组
       /* long count = checkItemDao.find(id);
        if(count>0){
            throw new RuntimeException("当前检查组被引用，不能删除")；
        }
        checkItemDao.deleteById(id);
    }*/
        try {
            checkItemDao.deleteById(id);
            return new Result();
        } catch (Exception e) {
            return new Result("当前检查组已被引用，不能删除");//有中间表
        }
    }

    @Override
    public CheckItem finById(Integer id) {
        return checkItemDao.finById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
         checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}